﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace eBlog.Common
{
    public class Pager
    {
        /// <summary>
        /// 初始化类
        /// </summary>
        public Pager(){}

     
            /// <summary>
            /// 分页导航条
            /// </summary>
            /// <param name="_RecordCount"></param>
            /// <param name="_PageSize"></param>
            /// <param name="_PageIndex"></param>
            /// <returns></returns>
          public static string PageInfo(int _RecordCount, int _PageSize, int _PageIndex)
          {
              
                string urlAll = HttpContext.Current.Request.Url.PathAndQuery;   // -- /admin/admin.aspx?id=dddd
                string url4 = HttpContext.Current.Request.Url.Query;    // -- ?id=ddd

                int next = 0, pre = 0, startpage = 0, endpage = 0;

                int _PageCount = (_RecordCount - 1) / _PageSize + 1; //获得页码

                string str_Page = "";

                if (_PageIndex < 1) { _PageIndex = 1; }

                if (_PageIndex > _PageCount) { _PageIndex = _PageCount; }

                next = _PageIndex + 1;

                pre = _PageIndex - 1;

                // 中间页起始序号
                startpage = (_PageIndex + 5) > _PageCount ? _PageCount - 9 : _PageIndex - 4;

                if (startpage < 1) { startpage = 1; }
                // 中间页终止序号
                endpage = _PageIndex < 5 ? 10 : _PageIndex + 5;
                if (endpage > _PageCount) { endpage = _PageCount; }

                str_Page += "<em>&nbsp;" + _RecordCount + "&nbsp;</em>";

                str_Page += _PageIndex > 1 ? "<a href=\"" + UrlOper.AutoAddParam(urlAll, "page", "1") + "\">&lsaquo;&lsaquo;&lsaquo;</a> <a href=\"" + UrlOper.AutoAddParam(urlAll, "page", pre.ToString()) + "\">&lsaquo;&lsaquo;</a>" : " ";

                // 中间页循环显示页码
                for (int i = startpage; i <= endpage; i++) 
                {
                    str_Page += _PageIndex == i ? "<strong>" + i + "</strong>" : "<a href=\"" + UrlOper.AutoAddParam(urlAll, "page", i.ToString()) + "\">" + i + "</a>";
                }
                str_Page += _PageIndex != _PageCount ? "<a href=\"" + UrlOper.AutoAddParam(urlAll, "page", _PageCount.ToString()) + "\" class=\"nxt\">&rsaquo;&rsaquo;&rsaquo;</a>" : " ";
                return str_Page;

          }

          /// <summary>
          /// 分页导航条
          /// </summary>
          /// <param name="_RecordCount"></param>
          /// <param name="_PageSize"></param>
          /// <param name="_PageIndex"></param>
          /// <returns></returns>
          public static string PageInfo2(int _RecordCount, int _PageSize, int _PageIndex)
          {
              string NavTpl="";

               NavTpl+=" <span class=\"gofirst\">{first}</span>";
               NavTpl+=" <span class=\"goprev\">{pre}</span>";
               NavTpl+=" <span class=\"standard\">{each}</span>";
               NavTpl+=" <span class=\"gonext\">{next}</span>";
               NavTpl+=" <span class=\"golast\">{last}</span>";



              string urlAll = HttpContext.Current.Request.Url.PathAndQuery;   // -- /admin/admin.aspx?id=dddd
              string url4 = HttpContext.Current.Request.Url.Query;    // -- ?id=ddd

              int next = 0, pre = 0, startpage = 0, endpage = 0;

              int _PageCount = (_RecordCount - 1) / _PageSize + 1; //获得页码

              string str_Page = "";

              if (_PageIndex < 1) { _PageIndex = 1; }

              if (_PageIndex > _PageCount) { _PageIndex = _PageCount; }

              next = _PageIndex + 1;

              pre = _PageIndex - 1;

              // 中间页起始序号
              startpage = (_PageIndex + 5) > _PageCount ? _PageCount - 9 : _PageIndex - 4;

              if (startpage < 1) { startpage = 1; }
              // 中间页终止序号
              endpage = _PageIndex < 5 ? 10 : _PageIndex + 5;
              if (endpage > _PageCount) { endpage = _PageCount; }

              //str_Page += "<em>&nbsp;" + _RecordCount + "&nbsp;</em>"; //记录总数

              string tplFirst = _PageIndex > 1 ? "<a href=\"" + UrlOper.AutoAddParam(urlAll, "page", "1") + "\"> 第一页</a>" : "<strong>第一页</strong>";
              string tplPre = _PageIndex > 1 ? "<a href=\"" + UrlOper.AutoAddParam(urlAll, "page", pre.ToString()) + "\">上一页</a>" : "<span>上一页</span>";

              // 中间页循环显示页码
              for (int i = startpage; i <= endpage; i++)
              {
                  str_Page += _PageIndex == i ? "<strong>" + i + "</strong>" : "<a href=\"" + UrlOper.AutoAddParam(urlAll, "page", i.ToString()) + "\">" + i + "</a>";
              }
              string tplNext = _PageIndex != _PageCount ? "<a href=\"" + UrlOper.AutoAddParam(urlAll, "page", next.ToString()) + "\">下一页</a>" : "<span> 下一页</span>";
              string tplLast = _PageIndex != _PageCount ? "<a href=\"" + UrlOper.AutoAddParam(urlAll, "page", _PageCount.ToString()) + "\">末尾页</a>" : "<strong> 末尾页</strong>";

              NavTpl = NavTpl.Replace("{first}", tplFirst).Replace("{pre}", tplPre).Replace("{each}", str_Page).Replace("{next}", tplNext).Replace("{last}", tplLast);

              return NavTpl;

          }


     }

}
